<template>
    <div class="isDialog">
        <div class="main-container">
            <el-form :model="roleQuery" ref="form" label-width="80px" :inline="true">
                <el-form-item label="角色名称">
                    <el-input v-model="roleQuery.name" placeholder="请输入角色名称查询"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="roleQuery.description" placeholder="请输入备注查询"></el-input>
                </el-form-item>
                <el-form-item>
                    <!-- v-loading.fullscreen="fullscreenLoading" -->
                    <el-button type="primary" @click="openFullScree">查询</el-button>
                    <el-button @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
            <el-row :gutter="24" style="margin-left: 0;">
                <el-col :span="1.6" :offset="0">
                    <el-button type="primary" size="default" v-perm:system:role:insert="roleInsert">
                        <el-icon color="white">
                            <Plus />
                        </el-icon>
                        <span>新增</span>
                    </el-button>
                </el-col>
                <el-col :span="4" :offset="0">
                    <el-dropdown @command="handleCommand">
                        <el-button>
                            批量操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
                        </el-button>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item command="delete">
                                    <el-icon>
                                        <Delete />
                                    </el-icon>删除
                                </el-dropdown-item>
                                <el-dropdown-item command="enable">
                                    <el-icon>
                                        <CircleCheck />
                                    </el-icon>启用
                                </el-dropdown-item>
                                <el-dropdown-item command='disable'>
                                    <el-icon>
                                        <CircleClose />
                                    </el-icon>禁用
                                </el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>

                </el-col>
            </el-row>
            <el-table ref="tableRef" :data="page.records" border stripe max-height="489px" style="margin-top: 30px;">
                <el-table-column type="selection" width="40px" />
                <el-table-column type="index" label="#" :index="indexMethod" width="47px" align="center" />
                <el-table-column label="操作" width="264px" align="center">
                    <template #default="scope">
                        <el-button class="btn-operate" v-perm:system:role:grantFunction="roleGrantFunction"
                            type="primary" link size="default" :row='scope.row'>分配功能</el-button>
                        <el-divider direction="vertical" />
                        <el-button class="btn-operate" v-perm:system:role:grantButton="roleGtantButton" type="primary"
                            link size="default" :row="scope.row">分配按钮</el-button>
                        <el-divider direction="vertical" />
                        <el-button class="btn-operate" v-perm:system:role:update="roleUpdate" type="primary" link
                            size="default" :row="scope.row">编辑</el-button>
                        <el-divider direction="vertical" />
                        <el-popconfirm title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                            :icon="InfoFilled" @confirm="roleRemoveBatch(scope.row)">
                            <template #reference>
                                <el-button class="btn-operate" type="primary" link size="default">
                                    删除
                                </el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
                <el-table-column label="角色名称" prop="name" width="150px">
                </el-table-column>
                <!-- <el-table-column label="数据类型" prop="type" width="110px">
                    <template #header="{ column }">
                        <span class="header-icon">
                            <span class="header-text">
                                {{ column.label }}
                            </span>
                            <el-tooltip placement="top" effect="dark">
                                <template #content>
                                    1、全部数据-该角色对应的用户可以看到全部单据；
                                    <br />
                                    2、本机构数据-该角色对应的用户可以看到自己所在机构的全部单据；
                                    <br />
                                    3、个人数据 - 该角色对应的用户只可以看到自己的单据。单据是指采购入库、销售出库等
                                </template>
                                <el-icon size="16px">
                                    <QuestionFilled />
                                </el-icon>
                            </el-tooltip>
                        </span>
                    </template>
                    <template #default="scope">
                        <span>{{ scope.row.type }}</span>
                    </template>
                </el-table-column> -->
                <el-table-column label="价格屏蔽" prop="priceLimit" width="450px">
                    <template #header="{ column }">
                        <span class="header-icon">
                            <span class="header-text">
                                {{ column.label }}
                            </span>
                            <el-tooltip placement="top" effect="dark">
                                <template #content>
                                    价格屏蔽支持多选，主要用于控制首页和单据的价格屏蔽
                                </template>
                                <el-icon size="16px">
                                    <QuestionFilled />
                                </el-icon>
                            </el-tooltip>
                        </span>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="description" width="300px">
                </el-table-column>
                <el-table-column label="状态" prop="enabled">
                    <template #header="{ column }">
                        <span class="title">
                            {{ column.label }}
                        </span>
                    </template>
                    <template #default="scope">
                        <span class="title">
                            <span v-if="scope.row.enabled">
                                <el-tag type="success">启用</el-tag>
                            </span>
                            <span v-else>
                                <el-tag type="danger">禁用</el-tag>
                            </span>
                        </span>

                    </template>
                </el-table-column>
            </el-table>

            <el-pagination v-model:current-page="page.current" v-model:page-size="page.size"
                :page-sizes="[2, 5, 10, 20]" :default-page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper" :total="page.total" :page-count="page.pages"
                @change="getRoles()" />
        </div>

        <el-dialog :title="dialogTitle" v-model="visible" :destroy-on-close="true" draggable width="42%">
            <router-view @close="close" :data="data"></router-view>
        </el-dialog>
    </div>

</template>

<script setup>
import { ArrowDown, InfoFilled } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { withLoading } from '@/hooks/loading';
import { ref } from 'vue'
import useSys from '@/store/useSys';
import request from '@/api/request';
import router from '@/router';
let sys = useSys();
// 分页对象
let page = ref({})
// 弹窗挂载对象
const mainContainer = ref(null)
// 分页条件对象
let roleQuery = ref({})
// 弹窗标题
let dialogTitle = ref('')
let visible = ref(false)
let data = ref({})
// 表格
const tableRef = ref()

// loading加载
// const fullscreenLoading = ref(false)
const openFullScree = async () => {
    await withLoading(getRoles, null)()
}

// 获取角色列表
let getRoles = () => {
    let user = sys.sessionUser
    roleQuery.value.tenantId = user.tenantId
    roleQuery.value.current = page.value.current
    roleQuery.value.size = page.value.size

    request.get('role/page',
        {
            params: {
                ...roleQuery.value
            }
        }
    ).then(result => {
        if (result.code == 200) {
            page.value = result.data
        }
    })
}
getRoles()

// 批量操作
let handleCommand = (command) => {
    let tableData = tableRef.value.getSelectionRows();
    if (tableData.length <= 0) {
        ElMessage.warning("请选择一条记录")
        return;
    }
    let roleNames = []
    tableData.forEach((val, index) => {
        roleNames.push(val.name)
    })
    switch (command) {
        case "delete":
            roleRemoveBatch(null, roleNames);
            break;
        case "enable":
            upRoleEnabled(roleNames, true)
            break;
        case "disable":
            upRoleEnabled(roleNames, false)
            break;
    }
}

// 打开新增角色弹窗
let roleInsert = (p) => {
    dialogTitle.value = p.cname
    router.push(p.path)
    visible.value = true
}

/**
 * 角色授权
 * @param p 权限对象
 * @param row 当前行数据
 */
let roleGrantFunction = (p, row) => {
    dialogTitle.value = p.cname
    visible.value = true
    router.push(p.path)
    data.value = row
}
/**
 * 分配按钮
 */
let roleGtantButton = (p, row) => {
    dialogTitle.value = p.cname
    visible.value = true
    router.push(p.path)
    data.value = row
}
/**
 * 编辑角色信息
 */
let roleUpdate = (p, row) => {
    dialogTitle.value = '编辑'
    visible.value = true
    router.push(p.path)
    data.value = row
}

let roleRemoveBatch = (row, array) => {
    let roleNames = []
    if (array) {
        roleNames = array
    } else {
        roleNames = [row.name]
    }
    console.log(roleNames);

    // request.delete(`role/roleRemoveBatch/${roleNames}`).then(resp => {
    //     if (resp.code == 200) {
    //         openFullScree()
    //     }
    // })
}

let upRoleEnabled = (roleNames, enabled) => {
    request.put(`role/upRoleEnabled/${roleNames}/${enabled}`).then(resp => {
        if (resp.code == 200) {
            openFullScree()
        }
    })
}

// 重置按钮
let reset = () => {
    roleQuery = ref({
        pageNo: 1,
        pageSize: 5,
        tenantId: 9
    })
    // getRoles()
    openFullScree()
}

let close = () => {
    visible.value = false;
    getRoles()
}

const indexMethod = (index) => {
    return index + 1;
}
</script>

<style scoped>
/* 深度选择器 */
:deep(.el-tooltip__trigger:focus-visible) {
    outline: unset;
}

.btn-operate {
    background-color: white;
    border: 0px solid;
    color: #40a9ff;
    padding: 0px;
}

.header-icon {
    display: flex;
    align-items: center;
}

.title {
    display: flex;
    justify-content: center;
    align-items: center;
}

.header-text {
    margin-right: 10px;
}

.el-pagination {
    margin-top: 20px;
}
</style>